<?php
/*
 * @author eBIZ Team <dev@jungo.com.cn>
 * @copyright  Copyright (c) , ShenZhen JunGO Technology Co., Ltd. All rights reserved.
 * @license  Commercial authorization, unauthorized use and modification are prohibited
 * @url www.jungo.com.cn
 */

namespace ckeeper\dao;


use ckeeper\db\Model;
use ckeeper\entity\RegionEntity;
use Selibra\ORM\Annotations\Entity;
use Selibra\ORM\Annotations\Table;

/**
 * 地区dao
 * Class RegionDaoImpl
 * @package Services\System\Dao\Impl
 */
#[Entity(RegionEntity::class)]
#[Table('ebiz_region')]
class RegionDao extends Model
{
    /**
     * 获取所有地区数据
     * @return array
     */
    public function getAllRegion(): array
    {
        return $this->select();
    }

    /**
     * 根据id获取地区数据
     * @param int $id
     * @return mixed|void
     */
    public function getRegionById(int $id)
    {
        $this->getCondition()->andWhere('id', '=', $id);
        return $this->get();
    }

    /**
     * 根据父级id获取地区数据
     * @param int $pid
     * @return mixed|void
     */
    public function getRegionByPid(int $pid)
    {
        $this->getCondition()->andWhere('pid', '=', $pid);
        return $this->select();
    }

    /**
     * @param string $name
     * @param int $level
     * @param int|null $pid
     * @return RegionEntity|null
     */
    public function getRegionByNameAndLevel(string $name, int $level, ?int $pid = null): ?RegionEntity
    {
        $this->getCondition()->andWhere('name', 'like', $name . "%")->andWhere('level', $level);
        if (!empty($pid)) {
            $this->getCondition()->andWhere('pid', $pid);
        }
        return $this->get();
    }
}
